package com.ssm.test.controller;

import com.ssm.test.model.FactoryBMW320;
import com.ssm.test.model.FactoryBMW523;
import com.ssm.test.model.Stu;
import com.ssm.test.service.StuService;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/stu")
public class StuController {

    @Autowired
    private StuService stuService;

    @Autowired
    private FactoryBMW320 factoryBMW320;

    @Autowired
    private FactoryBMW523 factoryBMW523;

    private Logger log = LogManager.getLogger(StuController.class);

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView showStudents() {
        ModelAndView modelAndView = new ModelAndView("/show");
        List<Stu> list = stuService.getStuList();
        Map<String, List<Stu>> map = new HashMap<String, List<Stu>>();
        map.put("stuList", list);
        modelAndView.addAllObjects(map);
        return modelAndView;
    }

    @RequestMapping(value = "/delete/{stuid}", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView deleteStudent(@PathVariable Integer stuid) {
        ModelAndView modelAndView = new ModelAndView("/show");
        log.info(stuService.deleteByPrimaryKey(stuid) + "条数据已被删除！");
        List<Stu> list = stuService.getStuList();
        Map<String, List<Stu>> map = new HashMap<String, List<Stu>>();
        map.put("stuList", list);
        modelAndView.addAllObjects(map);
        return modelAndView;
    }

    @RequestMapping(value = "/createCar", method = RequestMethod.POST)
    @ResponseBody
    public void createCar() {
        System.out.println("需要一辆A型发动机带A型空调.\n即将开始生产...\n");
        System.out.println(factoryBMW320.createEngine() + "\n" + factoryBMW523.createAircondition());
    }
}
